package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理Controller
 * 
 * @author shenkunlin
 * @date 2025-08-03
 */
@Tag(name = "设备管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;


    /****
     * 查询产品信息
     */
    @GetMapping("/queryProduct/{productKey}")
    public R<List<ServiceCapability>> queryProduct(@PathVariable("productKey")String productKey){
        return R.ok( deviceService.queryProduct(productKey) );
    }

    /***
     * 查询设备影子数据（设备详情）
     */
    @Operation(description = "查询设备影子数据（设备详情）")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<DeviceShadowVo>> queryServiceProperties(@PathVariable("iotId")String iotId){
        List<DeviceShadowVo> list = deviceService.queryServiceProperties(iotId);
        return R.ok(list);
    }

    /**
     * 根据设备ID查询设备详情
     */
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getInfo(@PathVariable("iotId")String iotId){
        DeviceDetailVo deviceDetailVo = deviceService.queryDeviceDetail(iotId);
        return R.ok(deviceDetailVo);
    }



    /****
     * register
     */
    @PostMapping("/register")
    public AjaxResult registerDevice(@RequestBody DeviceDto dto){
        deviceService.registerDevice(dto);
        return success();
    }


    /***
     * 回去所有产品列表
     */
    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct(){
        List<ProductVo> vos = deviceService.getAllProduct();
        return R.ok(vos);
    }


    /***
     * 同步产品数据到系统
     */
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
        return AjaxResult.success();
    }


    /**
     * 查询设备管理列表
     */
    @Operation(summary = "查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }


}
